package src
{
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;

	public class Screen_Lateral extends MovieClip
	{
		private var users:Array;
		private var myParent:Treppy;
		
		
		
		private var scrolling:Boolean = false;
		private var scrollInitPosY:Number;
		private var userInitPos:Number = 30;
		private var bg:MovieClip = new MovieClip();
		
		public function Screen_Lateral(users:Array, myParent:Treppy) {
			this.myParent = myParent;
			this.users = users;
			
			scrollInitPosY = scroll.y;			
			addListeners();
			addUsers();
			posUsers();
			
			
		}
		
		private function addUsers():void{
			addChildAt(bg,1);
			for (var i:uint=0;i < users.length ;i++){				
				bg.addChild(users[i]);
				users[i].fill();
			}
		}
		
		private function posUsers():void{	
			for (var i:uint=0;i < users.length ;i++){				
				users[i].y  = userInitPos + i*users[i].height;
				users[i].x = 0;
			}
		}
		
		private function addListeners():void{
			
			this["scroll"].addEventListener(MouseEvent.MOUSE_DOWN, startScrolling);
			myParent.stage.addEventListener(MouseEvent.MOUSE_UP,stopScrolling);
			
			this["save"].addEventListener(MouseEvent.CLICK, onClick);
			this["list"].addEventListener(MouseEvent.CLICK, onClick);
			this["shuffle"].addEventListener(MouseEvent.CLICK, onClick);
			this["up"].addEventListener(MouseEvent.CLICK, onClick);
			this["down"].addEventListener(MouseEvent.CLICK, onClick);
			this["close"].addEventListener(MouseEvent.CLICK, onClick);
			
		}
		
		private function onClick(e:MouseEvent):void{
			switch (e.currentTarget){
				case this["save"]: myParent.updateXML("write"); break;
				case this["list"]: sortXML();break;
				case this["shuffle"]: shuffleXML(); break;
				case this["close"]: myParent.updateXML("write");
									myParent.stage.nativeWindow.close();
									break;
			}
		}
		
		
		private function startScrolling(e:MouseEvent):void{
			//			
			var bounds:Rectangle = new Rectangle(scroll.x,scrollInitPosY,0,scrollBar.height-scroll.height);
			scrolling = true;
			this['scroll'].startDrag(false,bounds);	
			this['scroll'].addEventListener(Event.ENTER_FRAME, onEnterFrame);
		}
		
		private function onEnterFrame(e:Event):void{
			var numVisibleUsers:uint = 5;
			var movedScroll:Number = scroll.y - scrollInitPosY;
			var porcent:Number = movedScroll /(scrollBar.height-scroll.height);
			if (scrolling){				
				bg.y = -porcent*(bg.height - 640);
			}
		}
		
		private function stopScrolling(e:MouseEvent):void{
			this['scroll'].removeEventListener(Event.ENTER_FRAME, onEnterFrame);
			this['scroll'].stopDrag();
			scrolling = false;
		}
		
		
		private function sortXML():void{
			users.sortOn(["level","points","username"],[Array.DESCENDING, Array.DESCENDING, Array.CASEINSENSITIVE]);
			posUsers();
		}
		
		private function shuffleXML():void{
			var aux:User; var n:uint;
			for (var i:uint=0;i < users.length-1 ;i++){
				n = (i+1) + Math.floor(Math.random()*(users.length-(i+1)));
				aux = users[n];
				users[n] = users[i];
				users[i] = aux;
			}
			posUsers();
		}		
	}
}